home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / GameKit / gamekit-1 / ScoreKeeper.m < prev    next >
Text File  |  1995-06-12  |  5KB  |  231 lines

  1.  
  2. #import <gamekit/gamekit.h>
  3.  
  4. #define DEMOMODE [[[NXApp delegate] gameScreen] demoMode]
  5. #define TOPSCORE [highScoreController highestScore]
  6.  
  7. @interface ScoreKeeper(private)
  8. - (BOOL)_changeOKTo:(int)new;
  9. - _changedFrom:(int)old;
  10. @end
  11.  
  12. @implementation ScoreKeeper
  13.  
  14. - init
  15. {
  16.     [super init];
  17.     scoreCanGoNegative = NO;
  18.     score = 0;
  19.     return self;
  20. }
  21.  
  22. - appDidInit:sender        // forwarded by GameBrain -- just loads score
  23. {
  24.     if (!highScoreController)
  25.         highScoreController = [[NXApp delegate] highScoreController];
  26.     if (!bonusTrackerList) bonusTrackerList = [[List alloc] init];
  27.     if (!delegateList) delegateList = [[List alloc] init];
  28.     [self updateTopScoreText];
  29.     [self updateScoreText];
  30.     return self;
  31. }
  32.  
  33. - (int)currentScore { return score; }
  34.  
  35. - resetScore
  36. {
  37.     score = 0;
  38.     [[self updateScoreText] updateTopScoreText];
  39.     return self;
  40. }
  41.  
  42. - scoreCanGoNegative:(BOOL)flag
  43. {
  44.     scoreCanGoNegative = flag;
  45.     return self;
  46. }
  47.  
  48. - (int)addToScore:(int)amount
  49. {
  50.     int newScore = score + amount;
  51.     int oldScore = score;
  52.     if (![self _changeOKTo:newScore]) return 0;
  53.     score = newScore;
  54.     [self _changedFrom:oldScore];
  55.     [self updateScoreText];
  56.     if ((score > TOPSCORE) && !DEMOMODE) [self updateTopScoreText];
  57. #ifdef NOISYDEBUG
  58.     fprintf(stderr, "Score inc'ed from %d to %d (+%d).\n",
  59.             oldScore, newScore, amount);
  60. #endif
  61.     return amount;
  62. }
  63.  
  64. - (int)subtractFromScore:(int)amount
  65. {
  66.     BOOL scoreWasOver = (score > TOPSCORE);
  67.     int newScore = score - amount;
  68.     int oldScore = score;
  69.     if (![self _changeOKTo:newScore]) return 0;
  70.     score = newScore;
  71.     if ((!scoreCanGoNegative) && (score < 0)) score = 0;
  72.     [self updateScoreText];
  73.     if (scoreWasOver && (score < TOPSCORE) && !DEMOMODE)
  74.         [self updateTopScoreText];
  75.     [self _changedFrom:oldScore];
  76. #ifdef NOISYDEBUG
  77.     fprintf(stderr, "Score dec'ed from %d to %d (%d).\n",
  78.             oldScore, newScore, (score - oldScore));
  79. #endif
  80.     return (score - oldScore);
  81. }
  82.  
  83. - (BOOL)_changeOKTo:(int)new
  84. {
  85.     int i; id temp;
  86. #ifdef NOISYDEBUG
  87.     fprintf(stderr, "_changeOKTo: %u delegate(s)...\n", [delegateList count]);
  88. #endif
  89.     for (i=0; i<[delegateList count]; i++) {
  90.         temp = [delegateList objectAt:i];
  91.         if ([temp respondsTo:@selector(scoreWillChangeFrom:to:)]) {
  92. #ifdef NOISYDEBUG
  93.         fprintf(stderr,
  94. "    Sending -scoreWillChangeFrom:to: message to delegate of \"%s\" class.\n",
  95.                 [[temp class] name]);
  96. #endif
  97.             if (![temp scoreWillChangeFrom:score to:new]) return NO;
  98.         }
  99.     }
  100.     return YES;
  101. }
  102.  
  103. - _changedFrom:(int)old
  104. {
  105.     int i; id temp;
  106. #ifdef NOISYDEBUG
  107.     fprintf(stderr, "_changedFrom: %u delegate(s)...\n", [delegateList count]);
  108. #endif
  109.     for (i=0; i<[delegateList count]; i++) {
  110.         temp = [delegateList objectAt:i];
  111.         if ([temp respondsTo:@selector(scoreChangedFrom:to:)]) {
  112. #ifdef NOISYDEBUG
  113.             fprintf(stderr,
  114. "    Sending -scoreChangedFrom:to: message to delegate of \"%s\" class.\n",
  115.                     [[temp class] name]);
  116. #endif
  117.             [temp scoreChangedFrom:old to:score];
  118.         }
  119.     }
  120.     return self;
  121. }
  122.  
  123. - (GKTrackerId)addBonusTracker:newTracker
  124. {
  125.     [bonusTrackerList addObjectIfAbsent:newTracker];
  126.     return [bonusTrackerList indexOf:newTracker];
  127. }
  128.  
  129. - bonusTracker:(GKTrackerId)index
  130. {
  131.     if (index < 0) return nil;
  132.     return [bonusTrackerList objectAt:index];
  133. }
  134.  
  135. - (int)addBonusToScore:(GKTrackerId)index advance:(BOOL)flag
  136. {
  137.     int change;
  138.     if (index < 0) return 0;
  139.     change = [self addToScore:[[bonusTrackerList objectAt:index] bonusValue]];
  140.     if (flag) [[bonusTrackerList objectAt:index] advanceBonus];
  141.     return change;
  142. }
  143.  
  144. - (int)subtractBonusFromScore:(GKTrackerId)index retreat:(BOOL)flag
  145. {
  146.     int change;
  147.     if (index < 0) return 0;
  148.     change = [self subtractFromScore:
  149.             [[bonusTrackerList objectAt:index] bonusValue]];
  150.     if (flag) [[bonusTrackerList objectAt:index] retreatBonus];
  151.     return change;
  152. }
  153.  
  154. - resetBonus:(int)index
  155. {
  156.     if (index < 0) [bonusTrackerList makeObjectsPerform:@selector(resetBonus)];
  157.     else [[bonusTrackerList objectAt:index] resetBonus];
  158.     return self;
  159. }
  160.  
  161. - updateScoreText
  162. {
  163.     [scoreText setIntValue:score];
  164.     return self;
  165. }
  166.  
  167. - updateTopScoreText
  168. {
  169.     [topScoreText setIntValue:((TOPSCORE > score) ? TOPSCORE : score)];
  170.     return self;
  171. }
  172.  
  173. - setHighScoreController:controller
  174. {
  175.     highScoreController = controller;
  176.     return self;
  177. }
  178.  
  179. - setTopScoreText:textField
  180. {
  181.     topScoreText = textField;
  182.     return self;
  183. }
  184.  
  185. - setScoreText:textField
  186. {
  187.     scoreText = textField;
  188.     return self;
  189. }
  190.  
  191. - topScoreText
  192. {
  193.     return topScoreText;
  194. }
  195.  
  196. - scoreText
  197. {
  198.     return scoreText;
  199. }
  200.  
  201. - highScoreController
  202. {
  203.     return highScoreController;
  204. }
  205.  
  206. - delegateList
  207. {
  208.     return delegateList;
  209. }
  210.  
  211. - addDelegate:newDel
  212. {
  213. #ifdef NOISYDEBUG
  214.     fprintf(stderr, "ScoreKeeper adding delegate of class \"%s\"\n.",
  215.             [[newDel class] name]);
  216. #endif
  217.     return [delegateList addObject:newDel];
  218. }
  219.  
  220. - removeDelegate:oldDel
  221. {
  222. #ifdef NOISYDEBUG
  223.     fprintf(stderr, "ScoreKeeper removing delegate of class \"%s\"\n.",
  224.             [[oldDel class] name]);
  225. #endif
  226.     return [delegateList removeObject:oldDel];
  227. }
  228.  
  229.  
  230. @end
  231.